package cn.tedu.order.management.controller;

import cn.tedu.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.common.pojo.vo.PageData;
import cn.tedu.common.web.JsonResult;
import cn.tedu.order.management.pojo.param.OrderAddNewParam;
import cn.tedu.order.management.pojo.vo.OrderAdminListVO;
import cn.tedu.order.management.pojo.vo.OrderStaffListVO;
import cn.tedu.order.management.pojo.vo.UserOrderVO;
import cn.tedu.order.management.service.IOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/orders/")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    @PostMapping("{serialNumber}/{state}")
    @ApiOperation("修改")
    public JsonResult updateState1(@PathVariable Long serialNumber,
                         @PathVariable Integer state){
       orderService.updateState(serialNumber, state);
        return JsonResult.ok();
    }

   /* @PostMapping("")
    @ApiOperation("修改")
    public JsonResult updateState(Long serialNumber,Integer state){
        orderService.updateState(serialNumber, state);
        return JsonResult.ok();
    }*/


    @PostMapping("/create")
    @ApiOperation("创建订单")
    public JsonResult add(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                          @Valid OrderAddNewParam orderAddNewParam){
        Long orderId = orderService.create(currentPrincipal, orderAddNewParam);
        return JsonResult.ok(orderId);
    }

    @ApiOperation("管理员查询订单")
    @GetMapping("adminList")
    public JsonResult listForAdmin(Integer page){
        Integer pageNum = page;
        if (page == null || page < 1) {
            pageNum = 1;
        }
        PageData<OrderAdminListVO> list
                = orderService.listForAdmin(pageNum);

        return JsonResult.ok(list);
    }

    @ApiOperation("员工查询订单")
    @GetMapping("staffList")
    public JsonResult listForStaff(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,Integer page){
        Integer pageNum = page;
        if (page == null || page < 1) {
            pageNum = 1;
        }
        PageData<OrderStaffListVO> list
                = orderService.listForStaff(currentPrincipal,pageNum);

        return JsonResult.ok(list);
    }

    @ApiOperation("根据订单ID删除订单")
    @PostMapping("{id}")
    public JsonResult deleteById(@PathVariable Long id){
        orderService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("用户查看订单")
    @GetMapping("userId")
    private JsonResult userById(Long id){
        UserOrderVO userOrderVO = orderService.listUserById(id);
        return JsonResult.ok(userOrderVO);
    }

    @ApiOperation("用户查看订单列表")
    @GetMapping("")
    private JsonResult list(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal){
        List<UserOrderVO> list = orderService.list(currentPrincipal);
        return JsonResult.ok(list);
    }

}
